<template>
  <div>
    <el-steps :active="activeStep" finish-status="success" simple>
      <el-step title="验证身份"></el-step>
      <el-step title="绑定手机"></el-step>
    </el-steps>
    <div v-if="activeStep === 0" style="margin-top: 15px;">
      <!-- 第一步的内容 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="form.code" placeholder="请输入验证码"></el-input>

          <el-button
            class="get-code-btn"
            @click="sendCode"
            :disabled="isDisabled"
          >{{ this.isDisabled ? `${this.secondsLeft}秒后重发` : "发送验证码" }}</el-button>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button style="margin-top: 12px;" @click="nextOne" type="primary">下一步</el-button>
          <el-button @click="cancelDig">取 消</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div v-else-if="activeStep === 1" style="margin-top: 15px;">
      <!-- 第二步的内容 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="form.code" placeholder="请输入验证码"></el-input>

          <el-button
            class="get-code-btn"
            @click="sendCode"
            :disabled="isDisabled"
          >{{ this.isDisabled ? `${this.secondsLeft}秒后重发` : "发送验证码" }}</el-button>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button style="margin-top: 12px;" @click="nextOne" type="primary">确认修改</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>



<script>
export default {
  props: ["phoneX"],
  data() {
    return {
      activeStep: 0,
      isDisabled: false, // 初始不禁用按钮
      lastClickTime: 0, // 存储最近一次点击时间戳
      secondsLeft: 0, // 倒计时秒数
      form: {
        phone: this.phoneX,
        code: "",
      },
      rules: {
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 发送验证码
    async sendCode() {
      // 处理获取验证码
      if (!this.form.phone) {
        return;
      }
      if (this.form.phone !== this.phoneX && this.activeStep === 0) {
        return;
      }
      this.countdownClick();
      await this.$axios
        .post("/api/user/sendMsg?phone=" + this.form.phone)
        .then((res) => {
          if (res.data.code == 1) {
            this.$notify({
              title: "获取验证码成功",
              message: res.data.msg,
              type: "success",
            });
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((error) => {
          // 处理请求失败的错误
          this.$message.error(error.response.data.msg);
        });
    },
    async nextOne() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // 验证码验证通过，跳转到下一个页面
          // 在这里提交表单
          await this.$axios
            .put("/api/user/modifyPhone", this.form)
            .then((res) => {
              if (res.data.code === 1) {
                if (this.activeStep === 0) {
                  this.activeStep = 1;
                  this.isDisabled = false; // 不禁用按钮
                  localStorage.removeItem("lastClickTime");
                  this.form = {};
                } else if (this.activeStep === 1) {
                  this.activeStep = 2;
                  this.isDisabled = false; // 不禁用按钮
                  localStorage.removeItem("lastClickTime");
                  this.cancelDig("succ");
                  this.$message.success(res.data.msg);
                  this.activeStep = 0;
                  this.form.code = "";
                }
              } else if (res.data.code === 0) {
                this.$message.error("修改失败");
              }
            })
            .catch((error) => {
              // 处理请求失败的错误
              this.$message.error(error.response.data.msg);
            });
        } else {
          this.$message.error("请填写验证码");
          return false;
        }
      });
    },
    cancelDig(value) {
      this.$emit("my-event", false, value);
    },
    goToLogin() {
      this.$router.push("/loginRegistration");
    },
    // 倒计时60秒方法
    countdownClick() {
      // 点击触发60秒倒计时
      if (this.isDisabled) {
        return;
      }
      this.isDisabled = true; // 禁用按钮
      this.secondsLeft = 60; // 设置初始倒计时秒数
      this.lastClickTime = Date.now(); // 存储当前点击时间戳
      localStorage.setItem("lastClickTime", this.lastClickTime);
      this.countdown(); // 开始倒计时
    },
    // 开始倒计时
    countdown() {
      if (this.secondsLeft === 0) {
        this.isDisabled = false; // 解除禁用
        return;
      }
      this.secondsLeft--;
      setTimeout(() => {
        this.countdown();
      }, 1000); // 每一秒更新倒计时秒数
    },
    //获取时间戳
    getTimestamp() {
      const lastClickTime = localStorage.getItem("lastClickTime"); // 获取最近一次点击时间戳
      if (lastClickTime !== null && Date.now() - +lastClickTime < 60000) {
        // 如果距离上次点击时间小于60秒
        this.isDisabled = true; // 禁用按钮
        this.secondsLeft = Math.ceil(60 - (Date.now() - +lastClickTime) / 1000);
        this.countdown(); // 开始倒计时
      } else {
        this.isDisabled = false; // 不禁用按钮
        localStorage.removeItem("lastClickTime");
      }
    },
  },
  mounted() {
    this.getTimestamp();
  },
  created() {},
};
</script>

<style scoped>
.get-code-btn {
  float: right;
  margin-top: 5px;
}
</style>